package com.bd.scm.module.user.controller;


import com.bd.offices.excel.ExcelUtil;
import com.bd.scm.domain.dto.user.PersonnelFilesDto;
import com.bd.scm.domain.models.user.PersonnelFiles;
import com.bd.scm.module.user.service.ILeaveManagementService;
import com.bd.scm.module.user.service.IPersonnelFilesService;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/leaveManagement")
@Api("离职人员接口")
public class LeaveManagementController {
    @Resource
    private ILeaveManagementService leaveManagementService;
    @Resource
    private IPersonnelFilesService personnelFilesService;
    /**
     * 获取离职人员信息.
     *
     * @param personnelFilesDto 条件
     * @return
     */
    @RequestMapping("/getLeavePersonList")
    @ResponseBody
    @ApiOperation(value = "获取离职人员信息", httpMethod = "GET", protocols = "HTTP", notes = "获取离职人员信息", produces = "application/json")
    public Result getPersonList(@ApiParam(name = "personnelFilesDto", value = "人员档案实体类", required = true)
            @RequestBody PersonnelFilesDto personnelFilesDto) {
        return ResultUtil.ok(leaveManagementService.getLeavePersonList(personnelFilesDto));
    }

    /**
     * 复职
     *
     * @param personnelFilesDto 条件
     * @return
     */
    @RequestMapping("/recoveryOccupation")
    @ResponseBody
    @ApiOperation(value = "复职", httpMethod = "GET", protocols = "HTTP", notes = "复职", produces = "application/json")
    public Result recoveryOccupation(@ApiParam(name = "personnelFilesDto", value = "人员档案实体类", required = true)
            @RequestBody List<PersonnelFilesDto>  personnelFilesDto, HttpServletRequest request) throws UnknownHostException {
        String clientIp = InetAddress.getLocalHost().getHostAddress();
        String flag = this.leaveManagementService.recoveryOccupation(personnelFilesDto,clientIp);
        if(null != flag){
            return ResultUtil.error(flag);
        }else{
            return ResultUtil.ok();
        }
    }

    /**
     * 调动
     *
     * @param personnelFilesDto 条件
     * @return
     */
    @RequestMapping("/transferPerson")
    @ResponseBody
    @ApiOperation(value = "调动", httpMethod = "GET", protocols = "HTTP", notes = "调动", produces = "application/json")
    public Result transferPerson(@ApiParam(name = "personnelFilesDto", value = "人员档案实体类", required = true)
            @RequestBody List<PersonnelFilesDto>  personnelFilesDto, HttpServletRequest request) throws UnknownHostException {
        String clientIp = InetAddress.getLocalHost().getHostAddress();
        String flag = this.leaveManagementService.transferPerson(personnelFilesDto,clientIp);
        if(null != flag){
            return ResultUtil.error(flag);
        }else{
            return ResultUtil.ok();
        }
    }

    /**
     * 离职人员导出
     * @param depSerial
     * @param serchTxt
     * @param response
     * @return
     * @throws IOException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    @RequestMapping(value = "getleavePersonListsExcel")
    @ApiOperation(value = "离职人员导出", httpMethod = "GET", protocols = "HTTP", notes = "离职人员导出", produces = "application/json")
    public Result getleavePersonListsExcel(@ApiParam(name = "depSerial", value = "部门编号", required = true)
                                        String depSerial,
                                       String serchTxt,
                                       HttpServletResponse response) throws IOException, InstantiationException, IllegalAccessException {
        PersonnelFilesDto personnelFilesDto = new PersonnelFilesDto();
        personnelFilesDto.setCurrent(new Long(-1));
        personnelFilesDto.setPageSize(new Long(-1));

        personnelFilesDto.setDepSerial(depSerial);
        personnelFilesDto.setSerchTxt(serchTxt);
        List<PersonnelFiles> doorDefinition = leaveManagementService.getLeavePersonList(personnelFilesDto).getRecords();
        System.out.println(doorDefinition);
        ExcelUtil.ExportExcel("导出人员名单"+ UUID.randomUUID().toString(), response, doorDefinition, PersonnelFiles.class);
        return ResultUtil.ok();
    }
}
